<template>
  <div>
    <slot />
  </div>
</template>

<script>
import utils from './utils.js';

export default {
  name: 'qt-form',

  props: {
    model: Object,

    showMessage: {
      type: Boolean,
      default: true,
    },

    rules: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },

  watch: {
    rules: {
      immediate: true,
      deep: true,
      handler(value) {
        this.myRules = value || {};
      },
    },
  },

  data() {
    return {
      myRules: {},
    };
  },

  methods: {
    setRules(rules) {
      this.myRules = rules || {};
    },

    validate(callback) {
      utils.validate(this.model, this.myRules, callback, {
        showMessage: this.showMessage,
      });
    },

    validateField(props, callback) {
      utils.validateField(this.model, this.myRules, props, callback, {
        showMessage: this.showMessage,
      });
    },
  },
};
</script>
